package com.ctparking;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.*;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;


public class MappaParcheggi extends MapActivity {

	ArrayList<GeoPoint> parcheggi;
	   private MapController mc;
	   private LocationManager lm;
	   GeoPoint p;
	   MapView mapView;
	    private LocationListener locationListener;
	    
	    
	    class MapOverlay extends com.google.android.maps.Overlay
	    {
	    	 @Override
	         public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
	         {
	             super.draw(canvas, mapView, shadow);                   
	             for(int i = 0; i<parcheggi.size(); i++){
	            	 
	            	 //---translate the GeoPoint to screen pixels---
	            	 GeoPoint x= parcheggi.get(i);
	            	 Point screenPts = new Point();
	            	 mapView.getProjection().toPixels(x, screenPts);
	            	 //---add the marker---
	            	 Bitmap bmp = BitmapFactory.decodeResource(getResources(), Database.parkingList.get(i).getIcon());
	            	 canvas.drawBitmap(Bitmap.createScaledBitmap(bmp, 35, 35, true), screenPts.x, screenPts.y-50, null);
	             }         
	             return true;
	         }
	    	
	    	/*
	    	 @Override
	         public boolean onTouchEvent(MotionEvent event, MapView mapView) 
	         {   
	             //---when user lifts his finger---
	             if (event.getAction() == 1) {                
	                 GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
	                 Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
	                 try {
	                     List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6()  / 1E6, p.getLongitudeE6() / 1E6, 1);
	  
	                     String add = "";
	                     if (addresses.size() > 0) 
	                     {
	                         for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
	                            add += addresses.get(0).getAddressLine(i) + "\n";
	                     }
	  
	                     Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
	                 }
	                 catch (IOException e) {                
	                     e.printStackTrace();
	                 }   
	                 return true;
	             }
	             else                
	                 return false;
	         }*/        
	    } 
	    
	    
 /** Called when the activity is first created. */    
	@Override
 public void onCreate(Bundle savedInstanceState)
 {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.mappaparcheggi);
     mapView = (MapView) findViewById(R.id.mapView);
     mc = mapView.getController();
     parcheggi= new ArrayList<GeoPoint>();
     
 	for(int i = 0; i < Database.parkingList.size(); i++){
 		 
 		double lat = Database.parkingList.get(i).getLatitudine();
 	    double lng = Database.parkingList.get(i).getLongitudine();
 	    GeoPoint aux  = new GeoPoint((int) (lat ), (int) (lng ));
 	    parcheggi.add(aux);
 	    p=aux;
	}
	   	
     mc.animateTo(p);
     //---use the LocationManager class to obtain GPS locations---
     lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    
     locationListener = new MyLocationListener();
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
     
     mapView.setBuiltInZoomControls(true);
     mc.setZoom(14);
     
     //---Add a location marker---
     MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
     MapOverlay mapOverlay = new MapOverlay();
     List<Overlay> listOfOverlays = mapView.getOverlays();
     listOfOverlays.clear();
     listOfOverlays.add(mapOverlay);
     listOfOverlays.add(myLocationOverlay);
     myLocationOverlay.enableMyLocation(); 
     
 }
	@Override
    protected boolean isRouteDisplayed() {
        return false;
    }
	
	private class MyLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {

            if (loc != null) {                

                Toast.makeText(getBaseContext(), "Location changed : Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
                GeoPoint p = new GeoPoint((int) (loc.getLatitude() * 1E6), (int) (loc.getLongitude() * 1E6));
                mc.animateTo(p);
                mc.setZoom(14);                
                mapView.invalidate();
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String provider, int status, 
            Bundle extras) {
            // TODO Auto-generated method stub
        }
    }    
}
